package com.xh3140.android.widget.dialog.view;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;

public abstract class RootView<V extends View> extends LinearLayoutCompat {
    private final HeaderView mHeaderView;
    private final V mBodyView;
    private final FooterView mFooterView;

    public RootView(@NonNull Context context) {
        super(context);
        setOrientation(VERTICAL);
        setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        mHeaderView = initHeaderView(context);
        if (mHeaderView != null && mHeaderView.getVisibility() != GONE) {
            addView(mHeaderView);
        }
        mBodyView = initBodyView(context);
        if (mBodyView != null && mBodyView.getVisibility() != GONE) {
            addView(mBodyView);
        }
        mFooterView = initFooterView(context);
        if (mFooterView != null && mFooterView.getVisibility() != GONE) {
            addView(mFooterView);
        }
    }

    /**
     * 获取首部视图
     */
    @Nullable
    public HeaderView getHeaderView() {
        return mHeaderView;
    }

    @NonNull
    public HeaderView requireHeaderView() {
        final HeaderView headerView = getHeaderView();
        if (headerView == null) {
            throw new IllegalStateException("RootView<?> " + this + " does not have a HeaderView.");
        }
        return headerView;
    }

    /**
     * 获取主体视图
     */
    @Nullable
    public V getBodyView() {
        return mBodyView;
    }

    @NonNull
    public V requireBodyView() {
        final V bodyView = getBodyView();
        if (bodyView == null) {
            throw new IllegalStateException("RootView<?> " + this + " does not have a BodyView.");
        }
        return bodyView;
    }

    /**
     * 获取底部视图
     */
    @Nullable
    public FooterView getFooterView() {
        return mFooterView;
    }

    @NonNull
    public FooterView requireFooterView() {
        final FooterView footerView = getFooterView();
        if (footerView == null) {
            throw new IllegalStateException("RootView<?> " + this + " does not have a FooterView.");
        }
        return footerView;
    }

    /**
     * 初始化首部视图
     * 一些特殊的对话框不需要首部视图，直接返回null即可。
     */
    @Nullable
    protected abstract HeaderView initHeaderView(@NonNull Context context);

    /**
     * 初始化主体视图
     * 一些特殊的对话框不需要主体视图，直接返回null即可。
     */
    @Nullable
    protected abstract V initBodyView(@NonNull Context context);

    /**
     * 初始化底部视图
     * 一些特殊的对话框不需要底部视图，直接返回null即可。
     */
    @Nullable
    protected abstract FooterView initFooterView(@NonNull Context context);

    /**
     * 只在FootView上方加分割线
     */
    @Override
    protected boolean hasDividerBeforeChildAt(int childIndex) {
        final int childCount = getChildCount();
        final int showDividers = getShowDividers();
        if (showDividers < 0) {
            return childIndex > 0 && childCount > 1 && getChildAt(childIndex) == mFooterView;
        }
        if (childIndex == 0) {
            return (showDividers & SHOW_DIVIDER_BEGINNING) != 0;
        } else if (childIndex == childCount) {
            return (showDividers & SHOW_DIVIDER_END) != 0;
        } else if ((showDividers & SHOW_DIVIDER_MIDDLE) != 0) {
            for (int i = childIndex - 1; i >= 0; --i) {
                if (getChildAt(i).getVisibility() != GONE) {
                    return true;
                }
            }
        }
        return false;
    }
}
